package Handling;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletBuy
 */
@WebServlet(name = "ServletBuy", urlPatterns = {"/ServletBuy"})
public class ServletBuy extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public GameAdmin ga = new GameAdmin();   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletBuy() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Hallo");
		HttpSession session = request.getSession(true);
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String player =(String) session.getAttribute("PlayerID") ;
    	String game = (String) session.getAttribute("GameID");
    	int playerid = Integer.parseInt(player);
    	int gameid = Integer.parseInt(game);
    	
    	int fieldid;
		try {
			fieldid = ga.getFieldID(playerid, gameid);
			System.out.println(fieldid);
			String alert;
			alert = ga.buyField(playerid, fieldid, gameid);
		
			try {
				out.println( "{" + "\"alert\":"+ "\""+alert+"\"" + "}" );
			 } finally { out.close(); } 
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
